(function () {
    /*
    *Author Kwan
    *Date 2010-11-22
    *Function Upload class must import jquery.form.js
    */

    //check the class name , it will be replaced when existed
    if (window.Upload) {
        alert('variable Upload has been used,it will be replaced with _UpLoad');
        window._Upload = window.Upload;
    }

    //constructor
    window.Upload = function (param) {
        param = param instanceof Object ? param : {};
        this.cfg = param.cfg || {};
    }

    var pt = Upload.prototype;

    pt.fileName = '';

    pt.submit = function (fileName, form, action) {
        pt.fileName = fileName;
        var opt = { url: 'scissors.axd', data: { action: action, fileName: fileName },
            beforeSubmit: pt.showRequest,
            success: pt.showResponse
        };
        $('#' + form).ajaxSubmit(opt);
        return false;
    }
    pt.showRequest = function (obj) {
        $("#ajaxText").addClass('process').removeClass('errcolor').text('正在处理......').fadeIn();
        //return $('#' + obj).val().length > 0 ? true : false;
    }
    pt.showResponse = function (json) {
        var json = eval('(' + json + ')');
        if (json.success) {
            pt.bmpCutter(json.src, pt.fileName);
            $("#ajaxText").fadeOut();
        }
        else {
            $("#ajaxText").addClass('process').removeClass('errcolor').text(json.msg).fadeIn();
        }
    }

    pt.bmpCutter = function (src, fileName) {
        $('#container').fadeIn();
        $.fn.bitmapCutter({
            src: src,
            fileName: fileName,
            renderTo: '#container',
            cutterSize: { width: 90, height: 110 },
            onGenerated: function (src) {
                $('#container').hide();
                src = '/' + src;
                $("#rIMG").attr("src", src + '?' + Math.random());
                $("#xImg").val(src);
            },
            rotateAngle: 90,
            lang: { clockwise: '顺时针旋转{0}度.' }
        });
    }

    pt.popup = function popup(message) {
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        var dialogTop = 100;
        var dialogLeft = (maskWidth / 2) - ($('#dialog-box').width() / 2);
        $('#dialog-overlay').css({ height: maskHeight, width: maskWidth }).show();
        $('#dialog-box').css({ top: dialogTop, left: dialogLeft }).fadeIn();
        //$('#dialog-message').html(message);
        $('.btn-close').click(function () { $('#dialog-overlay, #dialog-box').hide(); return false; });
        $(window).resize(function () { if (!$('#dialog-box').is(':hidden')) popup(); });
    }

    pt.checkMail = function (str) { var pattern = /^([a-zA-Z0-9_-_.])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; return pattern.test(str); }
    pt.checkMobile = function (str) { var pattern = /^(?:13\d|15[789])|14\d-?\d{5}(\d{3}|\*{3})$/; return pattern.test(str); }
    pt.checkForm = function () {
		//alert($("#xBirth").attr("value").length);
        $("#returnText").fadeIn();
        if ($("#txtResumeName").val().length < 2) { $("#returnText").addClass('errcolor').removeClass('process').text('简历名称必须大于2个字符'); return false; }
        if ($("#txtRealName").val().length < 2) { $("#returnText").addClass('errcolor').removeClass('process').text('姓名必须大于2个字符'); return false; }
        if ($("#xBirth").attr("value").length < 2) { $("#returnText").addClass('errcolor').removeClass('process').text('请选择出生日期'); return false; }
        if (!pt.checkMail($("#txtEmail").val())) { $("#returnText").addClass('errcolor').removeClass('process').text('请输入正确的Email'); return false; }
        if (!pt.checkMobile($("#txtMobile").val())) { $("#returnText").addClass('errcolor').removeClass('process').text('请输入正确的移动电话号码'); return false; }
        if ($("#txtAddress").attr("value").length < 2) { $("#returnText").addClass('errcolor').removeClass('process').text('请填写现在的居住地'); return false; }
        return true;
    }

})();

window.upload = new Upload();
window._ = upload._;